$(function(){	
	//绑定点击事件
	bindFun();
	init();
});

var flag = true;

function init() {
	var username = localStorage.username;
	if(username!=''&&username!=null){
		$("#username").val(username);
		$("#remerber").attr("checked","checked");
	}
	padMarketing.ajaxNormal(
			'/pad_marketing/main/path.action',
			{'path':'domain'},
			function(result){
				changeImg("imgTwo");
				changeImg("img");	
				var data=result.result;
				if((data != "http://localhost:9000" && data != "http://portal.allianz.com.cn/" 
				&& data != "http://dev.allianz.com.cn/") || data == "https://pts.allianz.com.cn/") {
					$("#password").val("");
				} else {
					flag = false;
					$("#password").val("goodluck");
				}		
			}
	);
	$.ajax({
		url : '/pad_marketing/login/check-login.action',
		type : "post",
		data:{
			"toUrl":getQueryString("toUrl")
		} ,
		success : function(data) {
			if (data.status == 'success') {
				showLoadingPad("正在自动登录...");
				location.href=data.Url;
			}
		}
	})	

}

//点击事件
function bindFun(){
	$("#remerber").click(function(){
		var checked =  $("#remerber").attr("checked");
		if(checked=="checked"){
			var username = $("#username").val();
			if(username==""||username==null){
				alertMsg("请填写用户名");
				return;
			}
		}else{
			localStorage.removeItem("username");
		}
		
	});
	
	/*登陆点击事件*/
	$('#login').click(function() {
		var username=$("#username").val();
		var password=$("#password").val();
		var imgCode=$("#verify").val();
		if(username==""){
			alertMsg("请输入营销员工号");
			return;
		}
		if(password==""){
			alertMsg("请输入营销员工号密码");
			return;
		}
		if(imgCode==""&&flag){
			alertMsg("请输入验证码");
			return;
		}
		var param = {"userId":username,"password":password,"verify":imgCode};
		padMarketing.ajaxNormal(
				'/pad_marketing/login/login.action?toUrl='+getQueryString("toUrl"),
				param,
				function (data){
					if(data.ErrMsg){
						if(data.ErrMsg=="用户名密码错误。"){
							alertMsg(data.ErrMsg);
							$("#password").val("");
							$("#verify").val("");
							changeImg("img");
						}else{
							alertMsg(data.ErrMsg);
							$("#verify").val("");
							changeImg("img");					
						}
						return;
					}
					var check =  $("#remerber").attr("checked");
					if(check=="checked"){
						localStorage.setItem("username", username);
					}
					sendchannel_id();
					location.href=data.Url;
				},
				function(data){
					console.log(data);
				}
		);		
	});
	
	
	/*验证码点击事件*/
	$('#im').click(function() {
		changeImg("img");
	});
	$('#imgTwo').click(function() {
		changeImg("imgTwo");
	});
}
function clearU (me){

	$(me).prev().val("");
}
function changeImg(img){    
	if(img=="img"){
		var imgSrc = $("#"+img+"");  
	    var src = "/pad_marketing/xuan/verifyCode.action?time="+new Date();    
	    imgSrc.attr("src",src); 
	}else{
		var imgSrc = $("#"+img+"");  
	    var src = "/pad_marketing/xuan/verifyCodeTwo.action?time="+new Date();    
	    imgSrc.attr("src",src);
	}
       
} 

//密码相关
var tel;
var newTel;
var sixNum;
var user={};
//获取号码
function getTel(){
	var userName = $("#carrerId").val();
	if(userName==""||userName==null){
		alertMsg("请您先输入工号");
		return;
	}
	padMarketing.ajaxNormal(
			'/pad_marketing/login/getTel.action',
			{"userId":userName},
			function (data){	
				user = data;
				tel = data.agentTel;
				var newTel = tel.substring(0,3)+"****"+tel.substring(tel.length-4,tel.length);
				$("#telPre").attr("placeholder","请输入号码:"+newTel);
			},
			function(data){
				console.log(data);
			},
			false
	);
}




//定时清除sessionStorage
function clearSession(key,delay){
	setTimeout(function(){sessionStorage.removeItem(key)}, delay);
}

//发送短信
function askData(){
	var userId = $("#carrerId").val(); //员工ID
	var verVal   = $("#imgTwoVal").val();//验证码值
	var telPre   = $("#telPre").val();    //预留电话
	if(userId==""||userId==null){
		alertMsg("请输入您的营销员编号");
		return;
	}
	if(telPre==""||telPre==null||telPre!=tel){
		alertMsg("请正确输入您的预留电话");
		return;
	}
	sixNum = createSix();
	if(sessionStorage.getItem("sixNum")){
		sixNum = sessionStorage.getItem("sixNum");
	}else
	{
		sessionStorage.setItem("sixNum", sixNum);
		clearSession("sixNum",1000*5*60);
		
	}
	user.verifyCode=sixNum;
	user.verify = verVal;
	padMarketing.ajaxNormal(
			'/pad_marketing/login/createCode.action',
			user,
			function (data){	
				if(data.codeErrMsg){
					alertMsg(data.codeErrMsg);
					changeImg("imgTwo");
					return;
				}
				if(data.result){
					var result = JSON.parse(data.result);
					if(result.Status=="S"){
						handleSuccess();
					}else{
						alertMsg(result.ErrMsg);
					}
				}
			},
			function(data){

			}
	);
}
function createSix(){
	var Num=""; 
	for(var i=0;i<6;i++) 
	{ 
	Num+=Math.floor(Math.random()*10); 
	} 
	return Num;
}
//处理数据
function handleSuccess(){
	$("#first").hide();
	$("#second").show();
	$("#tel").html(newTel);
	$("#third").hide();
	$("#four").show();
	sendByTime();
}
//定时添加功能 
var timeFlag = false;
var timeInit;
function sendByTime(){
	$("#sendByTime").removeAttr("onClick");
	var timeNum = 60;
	 timeInit = setInterval(function(){
		timeFlag=true;
		$("#sendByTime").html(timeNum--+"S后重新发送");
		if(timeNum==-1){
			clearInterval(timeInit);
			timeFlag=false;
			$("#sendByTime").html("重新发送");
			$("#sendByTime").attr("onClick","askData()");
		}
	},1000);
} 

//确认动态码
function sureData(){
	var sixNumT =  $("#sixNum").val();
	if(sixNumT&&sixNumT== sixNum){
		$("#second").hide();
		$("#four").hide();
		$("#inputNew").show();
		$("#sureNew").show();
		$("#buttonNew").show();
	}else{
		alertMsg("您输入的动态码不正确,请重新输入");
		return;
	}
	
}
function changePassword(){
	var newPassword = $("#newPassword").val();
	var reNewPassword = $("#reNewPassword").val();
	if(newPassword!=null && newPassword!=""){
		if(newPassword!=reNewPassword){
			alertMsg("请您确定您填写的密码");
			return;
		}
	}else{
		alertMsg("请录入您的密码");
		return;
	}
	showLoadingPad();
	user.password = newPassword;

	padMarketing.ajaxNormal(
			'/pad_marketing/login/changePassword.action',
			user,
			function (data){	
				alertMsg("恭喜您修改成功");
    			closeDiv('passwordReset');
    			return;
			},
			function(data){

			}
	);
}
function checkCheck(me){
	var placeholder = $(me).attr("placeholder");
	if(placeholder==null||placeholder==""){
		alertMsg("请您先点击校验获得你预留的电话号码");
		return;
	}
}
function forgetPassword(){
	changeImg("imgTwo");
	$("#newPassword").val("");
	$("#reNewPassword").val("");
	$("#carrerId").val("");
	$("#telPre").val("");
	$("#imgTwoVal").val("");
	$("#sixNum").val("");
	$("#telPre").attr("placeholder","");
	$("#first").show();
	$("#third").show();
	$("#second").hide();
	$("#four").hide();
	$("#inputNew").hide();
	$("#sureNew").hide();
	$("#buttonNew").hide();
	var username = $("#username").val();
	if(username){
		$("#carrerId").val(username);
	}
	showDiv('passwordReset');
}
function closeTime(){
	if(timeFlag){
		clearInterval(timeInit);
		timeFlag=false;
	}
	closeDiv('passwordReset');
}